package com.zw.design.pattern.creational.structural.proxy.staticproxy;

import com.zw.design.pattern.creational.structural.proxy.IOrderService;
import com.zw.design.pattern.creational.structural.proxy.IOrderServiceImpl;
import com.zw.design.pattern.creational.structural.proxy.Order;
import com.zw.design.pattern.creational.structural.proxy.db.DataSourceContextHolder;

/****
 * 静态代理
 */
public class OrderServiceStaticProxy {

    private IOrderService iOrderService;


    public int saveOrder(Order order){
        beforeMethod(order);
        iOrderService=new IOrderServiceImpl();
        int i = iOrderService.saveOrder(order);
        afterMethod();
        return  i;
    }

    private void beforeMethod(Order obj)
    {
        Long userId=obj.getUserId();
        //开始业务操作
        int dbRouter= (int) (userId%2);
        System.out.println("dbRouter = 静态代理分配到 db" + dbRouter +"处理数据");
        //TODO 设置dataSource 这里有spring 分库的思想 ;
        DataSourceContextHolder.setDbType(String.valueOf(dbRouter));
        System.out.println("静态代理 before  code");

    }

    private void afterMethod(){
        System.out.println("静态代理 after code ");
    }

}
